<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_ijazah extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.kd_prodi';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$tahun = isset($_POST['tahun']) ? strval($_POST['tahun']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status, a.status_kerja,a.instansi,a.tahun_lulus');
			$this->db->select('b.nama_prodi, c.nama_dosen');
			$this->db->from('tbl_mahasiswa a');
			$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
			$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen','left');
			
			
			#Kimochi - otoritas
				#--Faklutas
			if($this->session->userdata('kd_fakultas')!=''){
				$this->db->where('b.kd_fakultas', $this->session->userdata('kd_fakultas'));
			}else{
				if($kd_fakultas != '')
					$this->db->where('b.kd_fakultas', $kd_fakultas);
			}
				#--Prodi
			if($this->session->userdata('kd_prodi')!=''){
				$this->db->where('a.kd_prodi', $this->session->userdata('kd_prodi'));
			}else{
				if($kd_prodi != '')
					$this->db->where('a.kd_prodi', $kd_prodi);
			}
			
			if($tahun != ''){
				$this->db->where('a.thn_angkatan', $tahun);
			}
			$this->db->where('a.status','Lulus');
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataByNIM($nim){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
		$this->db->where('a.nim', $nim);
		
		return $this->db->get();
	}
	
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function getDataCount($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_mahasiswa');
		
		if($status=='ma'){
			$this->db->where('status', 'Mahasiswa Aktif');
		}
		return $this->db->get()->num_rows();
	}
	
	/* function get_pdf(){
		# get data
		$this->db->flush_cache();		
		$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, a.status_kerja, a.instansi, a.tahun_lulus ');
		$this->db->from('tbl_mahasiswa a');
		$this->db->where('a.status','Lulus');
	//	$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
	//	$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen','left');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'NIM', 'width'=>20, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Mahasiswa', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Alamat', 'width'=>35, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Telepon', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Tahun Lulus', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Status Kerja', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Instansi', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->nim,
				$row->nama,
				$row->alamat,
				$row->no_telp,
				$row->tahun_lulus,
				$row->status_kerja,
				$row->instansi
			));
			$i++;
		}
		
		return $pdfdata;
		
	} */
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.alamat, a.no_telp, a.nama_ortu, a.alamat_ortu, a.pekerjaan, a.telp_ortu, a.status, a.kd_prodi,a.status_kerja,a.instansi');
		$this->db->select(' b.nama_prodi, d.kd_dosen, d.nama_dosen');
		$this->db->select(' c.kd_fakultas, c.nama_fakultas');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas','left');
		$this->db->join('ref_dosen d', 'd.kd_dosen = a.kd_dosen','left');
		
		$this->db->where('a.kd_mahasiswa', $kode);
		
		return $this->db->get();
	}
	
	function get_pdf($kode){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_mahasiswa, a.thn_angkatan, a.nim, a.nama, a.tempat_lahir, a.status,a.tahun_lulus, a.tgl_lahir');
			$this->db->select('b.nama_prodi, c.nama_fakultas, d.nama as nama_jenjang, e.tgl_yudisium, date_format(a.tgl_lahir, \'%d-%M-%Y\') as Lahir',false);
			$this->db->from('tbl_mahasiswa a');
			$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
			$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
			$this->db->join('ref_jenjang d', 'd.kd_jenjang = b.kd_jenjang');
			$this->db->join('tbl_sidang e', 'e.nim = a.nim', 'left');
			//$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen','left');
			
			$this->db->where('a.kd_mahasiswa', $kode);
			//$this->db->where('a.status','Lulus');
			
			// proses
			$result = $this->db->get();
		
			if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
					$data_pdf[] = $data;
				}
				return $data_pdf;           
			}
		
	}
}

?>